const Notification = require('electron-native-notification');
const url = require('url')
const path = require('path')
// Modules to control application life and create native browser window
const {
  webContents,
  dialog,
  app,
  BrowserWindow,
  Menu,
  Tray,
  ipcMain
} = require('electron')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
let helpWindow
let tray = null
let contents = null
const opts = {
  userAgent: "Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19"
}

function createhelpWindow() {
  helpWindow = new BrowserWindow({
    width: 520,
    height: 800,
    minimizable: false,
    show: false,
    webPreferences: {
      nodeIntegration: true
    }
  })
  helpWindow.loadURL(url.format({
    pathname: path.join(__dirname, 'index.html'),
    protocol: 'file:',
    slashes: true
  }))
  helpWindow.webContents.openDevTools()
  helpWindow.on('closed', function () {
    helpWindow = null
  })
  //mainWindow.hide()
  helpWindow.on('close', function () {
    mainWindow = null
    helpWindow = null
    app.quit()
  })
}

function createWindow() {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 520,
    height: 800,
    minimizable: false,
    webPreferences: {
      nodeIntegration: false
    }
  })
  const contextMenu = Menu.buildFromTemplate([{
      label: 'Show Window',
      click: () => {
        mainWindow.show()
        mainWindow.focus()
      }
    },
    {
      label: 'Quit',
      click: () => {
        app.isQuiting = true
        app.quit()
      }
    }
  ])
  const iconPath = path.join(__dirname, 'public/logo_icon.png')
  mainWindow.setIcon(iconPath)
  tray = new Tray(iconPath)
  tray.setToolTip("Gitee Desktop")
  tray.setContextMenu(contextMenu)


  mainWindow.loadURL('https://m.gitee.com/', opts)
  // Open the DevTools.
  //mainWindow.webContents.openDevTools()
  contents = mainWindow.webContents
  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    mainWindow = null
  })

  mainWindow.on('close', function (event) {
    if (!app.isQuiting) {
      event.preventDefault()
      mainWindow.hide()
    }
    return false
  })

  ipcMain.on('ajax-request', (e,args) => {
    console.log("ajax-request")
    contents.executeJavaScript(`$.ajax(${args})`)
  })
  ipcMain.on('ajax-get', (e,args) => {
    console.log(`$.get("${args.url}",${JSON.stringify(args.data)}, (res) => { return res})`)
    contents.executeJavaScript(`$.get("${args.url}",${JSON.stringify(args.data)}, (res) => { return res})`, true)
      .then((result) => {
        e.returnValue = result
      })
  })
  ipcMain.on('refresh-window', (e,args) => {
    mainWindow.webContents.reload()
  })
}

function initApplication(){
  createWindow()
  createhelpWindow()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', initApplication)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', function () {
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
    createhelpWindow()
  }
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.